package com.zuimei.zuimei.jsonutil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zuimei.zuimei.beans.CollectionsBean;
import com.zuimei.zuimei.beans.CategoryBean;
import com.zuimei.zuimei.beans.CategoryItemBean;
import com.zuimei.zuimei.beans.FindAppItem1Bean;
import com.zuimei.zuimei.beans.FindAppItem2Bean;
import com.zuimei.zuimei.beans.FindAppItem4Bean;
import com.zuimei.zuimei.beans.FindAppItem5Bean;
import com.zuimei.zuimei.beans.Item1DescBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jogger on 2017/1/16.
 * 解析发现应用
 */

public class ParseFindAppJson {
    /**
     * 精选数据
     */
    public static List<FindAppItem1Bean> getItem1List(String jsonString) {
        List<FindAppItem1Bean> findAppItem1BeanList = null;
        try {
            findAppItem1BeanList = new ArrayList<>();
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            JSONArray jsonArray_content = jsonObject_data.getJSONArray("content");
            Gson gson = new Gson();
            TypeToken<List<FindAppItem1Bean>> typeToken = new TypeToken<List<FindAppItem1Bean>>() {
            };
            findAppItem1BeanList = gson.fromJson(jsonArray_content.toString(), typeToken.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return findAppItem1BeanList;
    }

    /**
     * 最新页
     */
    public static List<FindAppItem2Bean> getItem2List(String jsonString) {
        List<FindAppItem2Bean> findAppItem2BeanList = null;
        try {
            findAppItem2BeanList = new ArrayList<>();
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            JSONArray jsonArray_apps = jsonObject_data.getJSONArray("apps");
            Gson gson = new Gson();
            TypeToken<List<FindAppItem2Bean>> typeToken = new TypeToken<List<FindAppItem2Bean>>() {
            };
            findAppItem2BeanList = gson.fromJson(jsonArray_apps.toString(), typeToken.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return findAppItem2BeanList;
    }

    /**
     * 周边数据
     */
    public static List<FindAppItem1Bean.AppsBean> getItem3List(String jsonString) {
        List<FindAppItem1Bean.AppsBean> findAppItem3BeanList = null;
        try {
            findAppItem3BeanList = new ArrayList<>();
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            JSONArray jsonArray_content = jsonObject_data.getJSONArray("media_articles");
            Gson gson = new Gson();
            TypeToken<List<FindAppItem1Bean.AppsBean>> typeToken = new
                    TypeToken<List<FindAppItem1Bean.AppsBean>>() {
                    };
            findAppItem3BeanList = gson.fromJson(jsonArray_content.toString(), typeToken.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return findAppItem3BeanList;
    }

    /**
     * 合辑数据
     */
    public static List<FindAppItem4Bean> getItem4List(String jsonString) {
        List<FindAppItem4Bean> findAppItem4BeanList = null;
        try {
            findAppItem4BeanList = new ArrayList<>();
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            JSONArray jsonArray_content = jsonObject_data.getJSONArray("albums");
            Gson gson = new Gson();
            TypeToken<List<FindAppItem4Bean>> typeToken = new TypeToken<List<FindAppItem4Bean>>() {
            };
            findAppItem4BeanList = gson.fromJson(jsonArray_content.toString(), typeToken.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return findAppItem4BeanList;
    }

    /**
     * 美友数据
     */
    public static List<FindAppItem5Bean> getItem5List(String jsonString) {
        List<FindAppItem5Bean> contentList = null;
        try {
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            JSONArray jsonArray_content = jsonObject_data.getJSONArray("users_rank");
            Gson gson = new Gson();
            TypeToken<List<FindAppItem5Bean>> typeToken = new
                    TypeToken<List<FindAppItem5Bean>>() {
                    };
            contentList = gson.fromJson(jsonArray_content.toString(), typeToken.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return contentList;
    }

    /**
     * 美友主页推荐
     */
    public static List<FindAppItem2Bean> getRecommandAppList(String jsonString) {
        List<FindAppItem2Bean> recommandAppList = null;
        try {
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            JSONArray jsonArray_app = jsonObject_data.getJSONArray("apps");
            TypeToken<List<FindAppItem2Bean>> typeToken = new TypeToken<List<FindAppItem2Bean>>() {
            };
            recommandAppList = new Gson().fromJson(jsonArray_app.toString(), typeToken.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return recommandAppList;

    }

    /**
     * 发现应用--精选详情
     */
    public static Item1DescBean getItem1Desc(String jsonString) {
        Item1DescBean item1DescBean = null;
        try {
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            TypeToken<Item1DescBean> typeToken = new TypeToken<Item1DescBean>() {
            };
            item1DescBean = new Gson().fromJson(jsonObject_data.toString(), typeToken.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return item1DescBean;

    }

    public static List<CollectionsBean> getCollectionsList(String jsonString) {
        List<CollectionsBean> collectionsList = null;
        try {
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            JSONArray jsonArray_collections = jsonObject_data.getJSONArray("collections");
            TypeToken<List<CollectionsBean>> typeToken = new TypeToken<List<CollectionsBean>>() {
            };
            collectionsList = new Gson().fromJson(jsonArray_collections.toString(), typeToken
                    .getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return collectionsList;
    }
    /**
     * 精选列表
     */
    public static List<CategoryBean> getCategoryList(String string){
        try {
            JSONObject jsonObject=new JSONObject(string);
            JSONObject data = jsonObject.getJSONObject("data");

            JSONArray jsonArray=data.getJSONArray("apps");
            TypeToken<List<CategoryBean>> typeToken=new TypeToken<List<CategoryBean>>(){};
            List<CategoryBean> list= new Gson().fromJson(jsonArray.toString(), typeToken.getType());
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
    /**
     * 精选單個列表
     */
    public static List<CategoryItemBean> getCategoryItemList(String string){
        try {
            JSONObject jsonObject=new JSONObject(string);
            JSONObject data = jsonObject.getJSONObject("data");

            JSONArray jsonArray=data.getJSONArray("apps");
            TypeToken<List<CategoryItemBean>> typeToken=new TypeToken<List<CategoryItemBean>>(){};
            List<CategoryItemBean> list= new Gson().fromJson(jsonArray.toString(), typeToken.getType());
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    public static FindAppItem2Bean getCollectionsDesc(String jsonString) {
        FindAppItem2Bean item2Bean = null;
        try {
            JSONObject jsonObject_all = new JSONObject(jsonString);
            JSONObject jsonObject_data = jsonObject_all.getJSONObject("data");
            item2Bean = new Gson().fromJson(jsonObject_data.toString(), FindAppItem2Bean.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return item2Bean;
    }
}
